<template>
  <Transition name="modal">
    <div v-if="isVisible" class="modal-container" :style="{ color: textColor }">
      <!-- 苹果风格工具栏 -->
      <div class="toolbar">
        <div class="toolbar-left">
          <button class="toolbar-button close" @click="closeModal" aria-label="关闭">
            <svg width="12" height="12" viewBox="0 0 12 12" fill="currentColor">
              <path d="M6 4.586L10.293.293a1 1 0 111.414 1.414L7.414 6l4.293 4.293a1 1 0 01-1.414 1.414L6 7.414l-4.293 4.293a1 1 0 01-1.414-1.414L4.586 6 .293 1.707A1 1 0 011.707.293L6 4.586z"/>
            </svg>
          </button>
          <button class="toolbar-button minimize" @click="$emit('minimize')" aria-label="最小化">
            <svg width="12" height="12" viewBox="0 0 12 12" fill="currentColor">
              <path d="M10.5 6.75h-9a.75.75 0 010-1.5h9a.75.75 0 010 1.5z"/>
            </svg>
          </button>
          <button class="toolbar-button maximize" @click="$emit('maximize')" aria-label="最大化">
            <svg width="12" height="12" viewBox="0 0 12 12" fill="currentColor">
              <path d="M1.5 3.75h9v4.5h-9v-4.5zm1.5 1.5v1.5h6v-1.5h-6z"/>
            </svg>
          </button>
        </div>
        <div class="toolbar-right">
          <span class="toolbar-title">{{ title }}</span>
        </div>
      </div>
      
      <!-- 内容区域 -->
      <div class="modal-content" :style="{ background: contentBackgroundColor, color: textColor }">
        <slot></slot>
      </div>
      
      <!-- 底部按钮区域 -->
      <div class="modal-footer" v-if="showFooter" :style="{ color: textColor }">
        <button class="cancel-button" @click="closeModal">取消</button>
        <button class="confirm-button" @click="$emit('confirm')">确认</button>
      </div>
    </div>
  </Transition>
</template>

<script setup lang="ts">
import { ref, watch, onMounted } from 'vue'

interface Props {
  modelValue: boolean
  title?: string
  showFooter?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  title: '提示',
  showFooter: false
})

const emit = defineEmits<{
  'update:modelValue': [value: boolean]
  confirm: []
  minimize: []
  maximize: []
}>()

const isVisible = ref(props.modelValue)
const contentBackgroundColor = ref('#ffffff')
const textColor = ref('#1d1d1f')

// 生成随机颜色
const generateRandomColor = () => {
  const letters = '0123456789ABCDEF'
  let color = '#'
  for (let i = 0; i < 6; i++) {
    color += letters[Math.floor(Math.random() * 16)]
  }
  return color
}

// 计算颜色亮度（0-255）
const getColorBrightness = (hexColor: string) => {
  // 移除#号
  const hex = hexColor.replace('#', '')
  
  // 解析RGB值
  const r = parseInt(hex.substr(0, 2), 16)
  const g = parseInt(hex.substr(2, 2), 16)
  const b = parseInt(hex.substr(4, 2), 16)
  
  // 计算亮度（使用加权平均值）
  return (r * 299 + g * 587 + b * 114) / 1000
}

// 根据背景色选择合适的文字颜色
const getContrastTextColor = (backgroundColor: string) => {
  const brightness = getColorBrightness(backgroundColor)
  // 如果背景色较亮，使用深色文字；如果背景色较暗，使用浅色文字
  return brightness > 128 ? '#000000' : '#ffffff'
}

// 设置随机内容区域背景色和合适的文字颜色
const setRandomColors = () => {
  contentBackgroundColor.value = generateRandomColor()
  textColor.value = getContrastTextColor(contentBackgroundColor.value)
}

onMounted(() => {
  setRandomColors()
})

watch(() => props.modelValue, (newValue) => {
  isVisible.value = newValue
  if (newValue) {
    setRandomColors()
  }
})

const closeModal = () => {
  isVisible.value = false
  emit('update:modelValue', false)
}
</script>

<style scoped>
.modal-container {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 
    0 10px 30px rgba(0, 0, 0, 0.15),
    0 1px 3px rgba(0, 0, 0, 0.1);
  width: 400px;
  max-width: 90vw;
  max-height: 80vh;
  overflow: hidden;
  z-index: 1000;
}

/* 苹果风格工具栏 */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 0.5px solid rgba(0, 0, 0, 0.1);
  background: linear-gradient(180deg, #f6f6f6 0%, #e8e8e8 100%);
  height: 40px;
  box-sizing: border-box;
}

.toolbar-left {
  display: flex;
  gap: 8px;
  align-items: center;
}

.toolbar-center {
  display: none;
}

.toolbar-right {
  display: flex;
  gap: 8px;
  align-items: center;
}

.toolbar-title {
  font-size: 13px;
  font-weight: 600;
  color: #1d1d1f;
  letter-spacing: -0.01em;
}

.toolbar-button {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  padding: 0;
}

.toolbar-button.close {
  background: #ff5f57;
  color: #ff5f57;
}

.toolbar-button.close:hover {
  background: #e0443e;
  transform: scale(1.1);
}

.toolbar-button.minimize {
  background: #ffbd2e;
  color: #ffbd2e;
}

.toolbar-button.minimize:hover {
  background: #e6a926;
  transform: scale(1.1);
}

.toolbar-button.maximize {
  background: #28ca42;
  color: #28ca42;
}

.toolbar-button.maximize:hover {
  background: #24b63a;
  transform: scale(1.1);
}

.toolbar-button:active {
  transform: scale(0.95);
}

.toolbar-button svg {
  opacity: 0;
  transition: opacity 0.2s ease;
}

.toolbar-button:hover svg {
  opacity: 1;
}

.toolbar-title {
  font-size: 13px;
  font-weight: 600;
  color: #1d1d1f;
  letter-spacing: -0.01em;
}

.modal-content {
  padding: 20px;
  max-height: calc(80vh - 120px);
  overflow-y: auto;
  color: #1d1d1f;
  line-height: 1.5;
  background: #ffffff;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 16px 20px;
  border-top: 0.5px solid rgba(0, 0, 0, 0.1);
  background: #f8f9fa;
}

.cancel-button,
.confirm-button {
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  border: none;
}

.cancel-button {
  background: transparent;
  color: #007aff;
}

.cancel-button:hover {
  background: rgba(0, 122, 255, 0.1);
}

.confirm-button {
  background: #007aff;
  color: white;
}

.confirm-button:hover {
  background: #0056cc;
  transform: translateY(-1px);
}

.confirm-button:active {
  transform: translateY(0);
}

/* 动画效果 */
.modal-enter-active,
.modal-leave-active {
  transition: all 0.3s ease;
}

.modal-enter-from {
  opacity: 0;
  transform: translate(-50%, -50%) scale(0.95);
}

.modal-leave-to {
  opacity: 0;
  transform: translate(-50%, -50%) scale(1.05);
}

.modal-enter-to,
.modal-leave-from {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}
</style>